package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired()
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);
        //设置用户id,指定当前是哪个用户的购物车数据
        Long userId = UserHolder.getUserId();
        shoppingCart.setUserId(userId);

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);
        //查询当前菜品或者套餐中是否在购物车中
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        if (dishId != null) {
            //当前添加到购物车的是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //当前添加到购物车的是套餐
            lqw.eq(ShoppingCart::getSetmealId, setmealId);
        }

        ShoppingCart one = shoppingCartService.getOne(lqw);
        if (one == null) {
            //如果不存在,则添加到购物车,数量默认是一
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        } else {
            //如果已经存在,就在原来数量基础上加一
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
        }
        return R.success(one);
    }

    /**
     * 查询购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R getShoppingCart() {
        List<ShoppingCart> list = shoppingCartService.list();
        return R.success(list);
    }

    /**
     * 取消购物车
     *
     * @return
     */
    @PostMapping("/sub")
    public R subShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        Long userId = UserHolder.getUserId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);

        //查询当前要取消的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        if (dishId != null){
            //当前要取消的是菜品
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else {
            //当前要取消的是套餐
            lqw.eq(ShoppingCart::getSetmealId,setmealId);
        }

        ShoppingCart one = shoppingCartService.getOne(lqw);
        //对数量进行-1操作
        Integer number = one.getNumber();
        number --;

        //判断number是否大于0,若小于等于0直接删除,若大于0则更新数量
        if (number > 0){
            one.setNumber(number);
            shoppingCartService.updateById(one);
        }else {
            shoppingCartService.remove(lqw);
            one = shoppingCart;
        }
        return R.success(one);
    }

    @DeleteMapping("/clean")
    public R cleanShoppingCart(){
        Long userId = UserHolder.getUserId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(lqw);
        return R.success("清空购物车成功!");
    }
}
